// index.js      
const app = getApp()      
Page({
   data: {
    // 考研日期
    examDate: new Date('2025-12-23').getTime(), // 存储为时间戳
    daysLeft: 0,
    theme: {},

    // 名言警句
    quotes: [
      "坚持就是胜利",
      "每天进步一点点",
      "天道酬勤",
      "不积跬步，无以至千里"
    ],
    currentQuote: "",
    
    // 任务列表
    tasks: [
      { id: 1, name: "数学", time: 0, timer: null, running: false, formattedTime: "00:00" },
      { id: 2, name: "英语", time: 30, timer: null, running: false, formattedTime: "00:30" },
      { id: 3, name: "专业课", time: 0, timer: null, running: false, formattedTime: "00:00" }
    ],
    
    // 下一个任务ID
    nextTaskId: 4,
    
    // 新增任务相关
    showAddTaskModal: false,
    newTaskName: ""
   },

   onLoad() {
    // 初始化格式化时间
    this.initFormattedTime();
    // 测试数据强制刷新
    this.setData({
      'tasks[0].time': 65,
      'tasks[1].time': 30 ,
    });
    // 获取全局主题
    this.setData({
      theme: app.globalData.theme
    });
    // 更新格式化时间
    this.updateFormattedTime();
   },

   onShow() {
    // 计算考研倒计时
    this.calculateDaysLeft();
    
    // 随机选择一条名言
    this.setRandomQuote();
    
    // 每天更新一次倒计时
    setInterval(() => {
      this.calculateDaysLeft();
    }, 1000 * 60 * 60 * 24);
   },

   // 初始化格式化时间
   initFormattedTime() {
    this.setData({
      tasks: this.data.tasks.map(task => ({
        ...task,
        formattedTime: this.formatTime(task.time)
      }))
    });
   },

   // 更新所有任务的格式化时间
   updateFormattedTime() {
    this.setData({
      tasks: this.data.tasks.map(task => ({
        ...task,
        formattedTime: this.formatTime(task.time)
      }))
    });
   },

   // 格式化时间显示（秒转换为MM:SS）
   formatTime(seconds) { 
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
   },

   // 计算距离考研的天数
   calculateDaysLeft() {
    const today = new Date();
    const examDate = new Date(this.data.examDate); // 从时间戳恢复
    const timeDiff = examDate - today;
    const daysLeft = Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
    this.setData({ daysLeft });
   },

   // 设置随机名言
   setRandomQuote() {
    const randomIndex = Math.floor(Math.random() * this.data.quotes.length);
    this.setData({ currentQuote: this.data.quotes[randomIndex] });
   },

   // 开始/停止计时器
   startTimer(e) {
    const taskId = e.currentTarget.dataset.id;
    const tasks = this.data.tasks.map(task => {
      if (task.id === taskId) {
        if (task.running) {
          clearInterval(task.timer);
          return { 
            ...task, 
            running: false, 
            timer: null,
            formattedTime: this.formatTime(task.time)
          };
        } else {
          const timer = setInterval(() => {
            this.incrementTaskTime(taskId);
          }, 1000);
          return { 
            ...task, 
            running: true, 
            timer,
            formattedTime: this.formatTime(task.time)
          };
        }
      } else {
        if (task.running) {
          clearInterval(task.timer);
          return { 
            ...task, 
            running: false, 
            timer: null,
            formattedTime: this.formatTime(task.time)
          };
        }
        return task;
      }
    });
    
    this.setData({ tasks });
   },

   // 增加任务时间
   incrementTaskTime(taskId) {
    this.setData({
      tasks: this.data.tasks.map(task => {
        if (task.id === taskId) {
          const newTime = task.time + 1;
          return { 
            ...task, 
            time: newTime,
            formattedTime: this.formatTime(newTime)
          };
        }
        return task;
      })
    });
   },

   // 显示添加任务弹窗
   showAddTaskDialog() {
    this.setData({
      showAddTaskModal: true,
      newTaskName: ""
    });
   },

   // 隐藏添加任务弹窗
   hideAddTaskDialog() {
    this.setData({
      showAddTaskModal: false
    });
   },

   // 输入新任务名称
   inputNewTaskName(e) {
    this.setData({
      newTaskName: e.detail.value
    });
   },

   // 确认添加新任务
   confirmAddTask() {
    if (this.data.newTaskName.trim() === "") {
      wx.showToast({
        title: '请输入任务名称',
        icon: 'none'
      });
      return;
    }

    const newTask = {
      id: this.data.nextTaskId,
      name: this.data.newTaskName,
      time: 0,
      timer: null,
      running: false,
      formattedTime: "00:00"
    };
    
    this.setData({
      tasks: [...this.data.tasks, newTask],
      nextTaskId: this.data.nextTaskId + 1,
      showAddTaskModal: false
    });
    
    wx.showToast({
      title: '添加成功',
      icon: 'success'
    });
   },

    // 选中任务
  selectTask(e) {
    const taskId = e.currentTarget.dataset.id;
    this.setData({
      selectedTaskId: taskId === this.data.selectedTaskId ? null : taskId
    });
  },

   // 删除任务
  deleteTask() {
    if (!this.data.selectedTaskId) {
      wx.showToast({
        title: '请先选择要删除的任务',
        icon: 'none'
      });
      return;
    }

    wx.showModal({
      title: '提示',
      content: '确定要删除这个任务吗？',
      success: (res) => {
        if (res.confirm) {
          const tasks = this.data.tasks.filter(task => task.id !== this.data.selectedTaskId);
          this.setData({ 
            tasks,
            selectedTaskId: null 
          });
          
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  },
   onAddButtonClick() {
     this.showAddTaskDialog();
 },
   onDeleteButtonClick(e) {
     this.deleteTask(e);
  }       
});      
